package com.itheima.main;

import com.itheima.bean.Menu;
import com.itheima.bean.Order;
import com.itheima.bean.Restaurant;
import com.itheima.bean.User;
import com.itheima.exception.ExceptionEnum;
import com.itheima.exception.UserException;
import com.itheima.service.*;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TakeoutSystem {
    private static TakeoutSystem instance;

    //登录用户
    private User loginUser;
    private Scanner scanner = new Scanner(System.in);
    private UserService userService = new UserServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    private RestaurantService restaurantService = new RestaurantServiceImpl();

    private TakeoutSystem() throws Exception {
        // 初始化餐厅和菜单信息
        Menu menu1 = new Menu("红烧肉", 25.0);
        Menu menu2 = new Menu("宫保鸡丁", 22.0);
        Menu menu3 = new Menu("鱼香肉丝", 20.0);
        Menu menu4 = new Menu("蚂蚁上树", 18.0);
        Menu menu5 = new Menu("水煮鱼", 28.0);
        List<Menu> menuList1 = Arrays.asList(menu1, menu2, menu3);
        List<Menu> menuList2 = Arrays.asList(menu2, menu3, menu4);
        List<Menu> menuList3 = Arrays.asList(menu3, menu4, menu5);
        //给餐厅添加菜品
        this.addRestaurant("餐厅1", menuList1);
        this.addRestaurant("餐厅2", menuList2);
        this.addRestaurant("餐厅3", menuList3);
    }

    public static TakeoutSystem getInstance() throws Exception {
        if (instance == null) {
            synchronized (TakeoutSystem.class) {
                if (instance == null) {
                    instance = new TakeoutSystem();
                }
            }
        }
        return instance;
    }

    //添加餐厅
    public void addRestaurant(String name, List<Menu> menuList) throws Exception {
        restaurantService.addRestaurant(name, menuList);
    }
    //用户下单操作
    public void placeOrder() throws Exception {
        System.out.println("请录入要下单的餐厅的名称: ");
        final String restaurantName = scanner.next();
        final Restaurant restaurant = restaurantService.browseRestaurant(restaurantName);
        if (restaurant == null) {
            System.out.println("没有查询到此餐厅，请重试.");
        } else {
            restaurant.getMenuList().forEach(menu -> System.out.println(menu.toString()));

            // 选择菜品
            System.out.println("请录入要下单的菜品，菜品之间以逗号分隔. ");
            final String foods = scanner.next();
            final String[] splits = foods.split(",");
            List<String> names = new ArrayList<>();
            Collections.addAll(names, splits);
            // 下单
            final String orderID = orderService.placeOrder(this.loginUser, restaurant.getName(), names);
            if (orderID.equals("no")) {
                System.out.println("下单失败! ");
            } else {
                System.out.println("下单成功，订单号：" + orderID);
            }
        }
    }
    //查看订单
    public void viewOrder() throws UserException, IOException {
        System.out.println("请录入要查询订单号: ");
        final String orderID = scanner.next();
        final Order order = orderService.viewOrder(orderID);
        if (order == null) {
            System.out.println("没有查询到这个订单，请检查订单号.");
        } else {
            System.out.println("订单号：" + orderID);
            System.out.println("用户名：" + order.getUser().getUsername());
            System.out.println("订单金额：" + order.getTotalAmount());
            System.out.println("下单时间：" + order.getCreateTime());
            System.out.println("菜单列表: ");
            order.printMenuList();
        }
    }

    //注册操作
    public void register() {
        try {
            // 1. 用户名
            System.out.println("请录入注册的用户名");
            final String username = scanner.next();

            // 2. 密码
            System.out.println("请录入注册的密码");
            final String password = scanner.next();
            // 3. 注册
            userService.register(new User(username, password));

        } catch (UserException e) {
            System.out.println(e.getErrorMsg());
            ExceptionEnum exceptionEnum = e.getExceptionEnum();
            System.out.println(exceptionEnum.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //登录操作
    public void login() {
        try {
            // 1. 用户名
            System.out.println("请录入登录的用户名");
            final String username = scanner.next();

            // 2. 密码
            System.out.println("请录入登录的密码");
            final String password = scanner.next();

            // 3. 登录操作
            final User logined = userService.login(username, password);
            if (logined != null) {
                this.loginUser = logined;
                showPanel();
            } else {
                throw new UserException(ExceptionEnum.USER_LOGIN_ERROR, "用户名或密码错误.");
            }

        } catch (UserException e) {
            System.out.println(e.getErrorMsg());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //查看餐厅菜单
    public void browseMenus() throws Exception {
        System.out.println("请录入要查询餐厅的名称: ");
        final String restaurantName = scanner.next();
        final Restaurant restaurant = restaurantService.browseRestaurant(restaurantName);
        if (restaurant == null) {
            System.out.println("没有查询到此餐厅，请重试.");
        } else {
            restaurant.getMenuList().forEach(menu -> System.out.println(menu.toString()));
        }
    }
    //浏览餐厅
    public void browseRestaurants() throws IOException {
        final List<Restaurant> restaurants = restaurantService.browseRestaurants();
        restaurants.forEach(restaurant -> System.out.println(restaurant.toString()));
    }

    //登录成功后展示功能
    public void showPanel() throws Exception {
        while (true) {
            System.out.println("欢迎:" + loginUser.getUsername());
            System.out.println("=====请选择操作=====");
            System.out.println("1、查看所有餐厅信息");
            System.out.println("2、查看餐厅菜品");
            System.out.println("3、下单");
            System.out.println("4、查看订单信息");
            System.out.println("按其它键退出操作界面");
            String type = scanner.next();
            switch (type) {
                case "1":
                    browseRestaurants();
                    break;
                case "2":
                    browseMenus();
                    break;
                case "3":
                    placeOrder();
                    break;
                case "4":
                    viewOrder();
                    break;
                default:
                    return;
            }
        }
    }

    //程序开始菜单
    public void start() {
        while (true) {
            System.out.println("请选择操作 1、注册用户 2、用户登录 3、按其它建退出系统");
                    String type = scanner.next();
            switch (type) {
                case "1":
                    this.register();
                    break;
                case "2":
                    this.login();
                    break;
                default:
                    System.out.println("欢迎下次使用本系统");
                    return;
            }
        }
    }
}
